package java_class_work.week06;

import java.util.*;

/**
 * @Auther: admin
 * @Date: 2022/10/17 16:58
 * @Description: 模拟斗地主发牌
 */
public class FightLandlord {
    public static void main(String[] args) {
        //定义花色
        String[] colors = {"♥", "♠", "♦", "♣"};
        String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        ArrayList<String> array = new ArrayList<>();
        for (String color : colors)
            for (String number : numbers)
                array.add(color + number);
        array.addAll(Arrays.asList("q小王", "k大王"));
        //洗牌-打乱
        Collections.shuffle(array);
        //发牌
        ArrayList<String> player1 = new ArrayList<>(17),
                player2 = new ArrayList<>(17),
                player3 = new ArrayList<>(17),
                dipai = new ArrayList<>(3);

        int i = 0;
        while (i < array.size() - 3) {
            player1.add(array.get(i++));
            player2.add(array.get(i++));
            player3.add(array.get(i++));
        }
        while (i < array.size()) {
            dipai.add(array.get(i++));
        }
        look("周润发", xipai(player1));
        look("周星驰", xipai(player2));
        look("刘德华", xipai(player3));
        look("底牌", xipai(dipai));
    }

    /**
     * 打印牌
     *
     * @param name
     * @param puke
     */
    static void look(String name, List<String> puke) {
        System.out.print(name + ":");
        puke.forEach(s -> System.out.print(s + ","));
        System.out.println();
    }

    /**
     * 整理顺序
     *
     * @param puke
     * @return
     */
    static List<String> xipai(List<String> puke) {
        // Collections.sort(puke, new Comparator<String>() {
        //     @Override
        //     public int compare(String o1, String o2) {
        //         return convert(o1)-convert(o2);
        //     }
        // });
        // Collections.sort(puke, (s1,s2) -> convert(s1)-convert(s2));
        // Collections.sort(puke, Comparator.comparingInt(s -> convert(s)));
        Collections.sort(puke, Comparator.comparingInt(FightLandlord::convert));
        return puke;
    }

    /**
     * 转换为 大小权值
     *
     * @param t
     * @return
     */
    static int convert(String t) {
        t = t.substring(1);
        switch (t) {
            case "J":
                return 11;
            case "Q":
                return 12;
            case "K":
                return 13;
            case "A":
                return 14;
            case "2":
                return 15;
            case "小王":
                return 16;
            case "大王":
                return 17;
            default:
                return Integer.parseInt(t);
        }
    }
}
